package com.qf.anno.ioc;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

import javax.servlet.Filter;

public class XmlConfig extends AbstractDispatcherServletInitializer {
    /**
     * 创建springmvc的ioc容器
     * @return
     */
    @Override
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext mvcIoc = new AnnotationConfigWebApplicationContext();
        mvcIoc.register(SpringMVCConfig.class);
        return mvcIoc;
    }

    /**
     * 配置前端控制器的url-pattern
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    /**
     * 创建service层的ioc容器
     * @return
     */
    @Override
    protected WebApplicationContext createRootApplicationContext() {
        AnnotationConfigWebApplicationContext ioc = new AnnotationConfigWebApplicationContext();
        ioc.register(SpringConfig.class);
        return ioc;
    }

    /**
     * 配置拦截DispatcherServlet的过滤器
     * @return
     */
    @Override
    protected Filter[] getServletFilters() {

        CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
        encodingFilter.setEncoding("utf-8");
        encodingFilter.setForceEncoding(true);

        return new Filter[]{encodingFilter};
    }
}
